package com.ball.dev.service.impl;

import com.ball.common.dtos.ResponseResult;
import com.ball.common.enums.AppHttpCodeEnum;
import com.ball.dev.entity.po.BallButton;
import com.ball.dev.entity.po.PostComment;
import com.ball.dev.entity.vo.ButtonVo;
import com.ball.dev.mapper.BallButtonMapper;
import com.ball.dev.service.IBallButtonService;
import com.ball.dev.service.IPostCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 投篮教学详细信息展示 服务实现类
 * </p>
 *
 * @author lou
 * @since 2024-04-30
 */
@Service
@RequiredArgsConstructor
public class BallButtonServiceImpl extends ServiceImpl<BallButtonMapper, BallButton> implements IBallButtonService {

    private final IPostCommentService commentService;
    @Override
    public ResponseResult getBallButton(String number) {
        if (number == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        BallButton button = lambdaQuery()
                .eq(BallButton::getNumber, number)
                .eq(BallButton::getStatus, 0)
                .one();

        if (button == null){
            return ResponseResult.okResult("等下次开发");
        }

        List<PostComment> comments = commentService
                .lambdaQuery().eq(PostComment::getPostId, button.getId())
                .eq(PostComment::getStatus, 0).
                orderByDesc(PostComment::getCreateTime)
                .list();


        ButtonVo buttonVo = new ButtonVo();
        buttonVo.setId(button.getId());
        buttonVo.setPhoto(button.getPhoto());
        buttonVo.setData(comments);


        return ResponseResult.okResult(buttonVo);
    }

    @Override
    public ResponseResult addBallButton(BallButton button) {
        button.setStatus(0);
        button.setCreateTime(LocalDateTime.now());
        return  save(button) ?
                ResponseResult.okResult("成功") : ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }

    public void addBallButton1(BallButton button) {
        button.setStatus(0);
        button.setCreateTime(LocalDateTime.now());
        System.out.println(save(button));
    }
}
